package com.robcity.platform.application;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.robcity.platform.dto.RobotLocationInfoSaveDTO;
import com.robcity.platform.enums.RobNameEnum;
import com.robcity.platform.param.RobotReqCommon;
import com.robcity.platform.repo.RobotLocationInfoRepo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author zhangsong
 * @Date 2024/8/8
 **/
@Slf4j
@Component
public class BootNettyUdpSimpleChannelInboundHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    @Resource
    RobotLocationInfoRepo robotLocationInfoRepo;
    @Resource
    RobotEventInfoApplication robotEventInfoApplication;
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        try {
            String strdata = msg.content().toString(CharsetUtil.UTF_8);
            //打印收到的消息
            log.info("---------------------receive data--------------------------");
            log.info(strdata);
            JSONObject jsonObject = JSONUtil.parseObj(strdata);
            RobotReqCommon common = jsonObject.get("common", RobotReqCommon.class);
            RobotLocationInfoSaveDTO robotLocationInfoDTO =jsonObject.get("data", RobotLocationInfoSaveDTO.class);
            if("working".equals(common.getInfo())){
                robotLocationInfoDTO.setWorkStatus(1);
            }else if("success".equals(common.getInfo())){
                robotLocationInfoDTO.setWorkStatus(2);
            }
            if(StringUtils.isNotBlank(common.getPlaId())) {
                robotLocationInfoDTO.setTaskId(common.getPlaId());
            }
            if(common.getMsgType()!=null){
                robotLocationInfoDTO.setTaskType(String.valueOf(common.getMsgType()));
            }
            robotLocationInfoRepo.insert(robotLocationInfoDTO);
            //检测是否进入重点设施巡视区域
            if(RobNameEnum.ONE.getCode().equals(robotLocationInfoDTO.getRob_id())){
                robotEventInfoApplication.RobotCheckAreaHandle(common,robotLocationInfoDTO);
            }
            //检测无人机是否到达事件区域
            if(RobNameEnum.FOUR.getCode().equals(robotLocationInfoDTO.getRob_id())){
                robotEventInfoApplication.uavCheckAreaHandle(common,robotLocationInfoDTO);
            }
            //有异常事件
            if(StringUtils.isNotBlank(robotLocationInfoDTO.getIncFlag())&&"1".equals(robotLocationInfoDTO.getIncFlag())){
                log.info("---------------------产生事件--------------------------");
                robotEventInfoApplication.RobotEventInfoCreateHandle(common,robotLocationInfoDTO);
            }
            if(StringUtils.isNotBlank(robotLocationInfoDTO.getIncFlagUAV())&&"0".equals(robotLocationInfoDTO.getIncFlagUAV())){
                log.info("---------------------关闭事件--------------------------");
                robotEventInfoApplication.RobotEventInfoCloseHandle(common,robotLocationInfoDTO);
            }
            //收到udp消息后，可通过此方式原路返回的方式返回消息，例如返回时间戳
//            ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("ok", CharsetUtil.UTF_8), msg.sender()));
        } catch (Exception e) {
            log.error("udp消息处理异常",e);
        }
    }
}